<?php

class Authresource extends AppModel {

    var $belongsTo = array(
        'Authresourcegroup' => array(
            'className' => 'Authresourcegroup',
            'foreignKey' => 'authresourcegroup_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
    var $hasAndBelongsToMany = array(
        'Authusergroup' => array(
            'className' => 'Authusergroup',
            'joinTable' => 'authresources_authusergroups',
            'foreignKey' => 'authresource_id',
            'associationForeignKey' => 'authusergroup_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

    function add($name, $groupname) {
        if (empty($name) || empty($groupname))
            return INSUFFICIENT_PARAMETERS; // return error : error varible empty 

        $this->Authresourcegroup->recursive = -1;
        $arr_resourcegroup = $this->Authresourcegroup->find('first', array('conditions' => array('authresourcegroup.name' => $groupname)));

        if (!empty($arr_resourcegroup)) {
            $group_id = $arr_resourcegroup['Authresourcegroup']['id'];
            $this->recursive = -1;
            $arr_resourcegroup = $this->find('count', array('conditions' => array('authresource.name' => $name)));
            if ($arr_resourcegroup != 0) {
                return NAME_ALREADY_TAKEN; // return error : name already taken
            } else {
                $field = array('authresourcegroup_id' => $group_id,
                    'name' => $name);
                $this->create();
                if ($this->save($field)) {
                    return SUCCESS; // return : success
                } else {
                    return SAVE_ERROR; // return error : error saving
                }
            }
        } else {
            return NON_EXISTANT_NAME; // return error : register not found
        }
    }

    function edit($name, $newname, $newgroupname) {
        if (empty($name) || empty($newname) || empty($newgroupname))
            return INSUFFICIENT_PARAMETERS; // return error : error varible empty 

        $this->Authresourcegroup->recursive = -1;
        $arr_resourcegroup = $this->Authresourcegroup->find('first', array('conditions' => array('authresourcegroup.name' => $newgroupname)));
        if (!empty($arr_resourcegroup)) {
            $group_id = $arr_resourcegroup['Authresourcegroup']['id'];
            $this->recursive = -1;
            $arr_resource = $this->find('first', array('conditions' => array('Authresource.username' => $name)));
            if (empty($arr_resource))
                return NON_EXISTANT_NAME;
            else
                $resource_id = $arr_resource['Authuser']['id'];

            $this->recursive = -1;
            $arr_resource = $this->find('first', array('conditions' => array('Authresource.username' => $newname)));
            if (!empty($arr_user)) {
                if ($resource_id != $arr_resource['Authresource']['id']) {
                    return NAME_ALREADY_TAKEN; // return error : name already taken
                }
            }

            $field = array('id' => $resource_id,
                'authresourcegroup_id' => $group_id,
                'name' => $name);
            if ($this->save($field)) {
                return SUCCESS; // return : success
            } else {
                return SAVE_ERROR; // return error : error saving
            }
        } else {
            return NON_EXISTANT_NAME;
        }
    }

    function deleteData($name) {
        if (empty($name)) {
            return INSUFFICIENT_PARAMETERS;
        } else {
            $this->recursive = -1;
            $arr_user = $this->find('first', array('conditions' => array('authresource.username' => $name)));
            if (empty($arr_user)) {
                return NON_EXISTANT_NAME;
            } else {
                $resourceid = $arr_user['Authresource']['id'];
                if ($this->del($resourceid)) {
                    return SUCCESS; // return : success
                } else {
                    return SAVE_ERROR; // return error : error saving
                }
            }
        }
    }

    function listdata($field, $value, $operator, $orderfield, $orderby, $limit, $page, $recursive) {
        if (!$recursive)
            $recursive = -1;
        $order = array();
        $conditions = array();
        if ($orderfield) {
            if (!$orderby) {
                $orderby = "";
            }
            $order = array("Authresource." . $orderfield . " " . $orderby);
        }
        if ($field) {
            $wildcard = "";
            if (!$operator)
                $operator = "";
            else {
                switch (strtoupper($operator)) {
                    case "=":
                        $operator = "";
                        break;
                    case "!=":
                        $operator = "<>";
                        break;
                    case "<>":
                        $operator = "<>";
                        break;
                    case "LIKE":
                        $operator = "LIKE";
                        $wildcard = "%";
                        break;
                    case "<":
                        $operator = "<";
                        break;
                    case ">":
                        $operator = ">";
                        break;
                    default:
                        $operator = "";
                        break;
                }
            }
            $conditions = array("Authresource." . $field . " " . $operator => $wildcard . $value . $wildcard);
        }
        if (!$limit) {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'page' => $page);
        }
        else {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit, 'page' => $page);
        }
        $this->recursive = $recursive;
        $resultsearch = $this->find('list', $params);
        return $resultsearch;
    }

    function getdata($field, $value, $operator, $orderfield, $orderby, $limit, $page, $recursive) {
        if (!$recursive)
            $recursive = -1;
        $order = array();
        $conditions = array();
        if ($orderfield) {
            if (!$orderby) {
                $orderby = "";
            }
            $order = array("Authresource." . $orderfield . " " . $orderby);
        }
        if ($field) {
            $wildcard = "";
            if (!$operator)
                $operator = "";
            else {
                switch (strtoupper($operator)) {
                    case "=":
                        $operator = "";
                        break;
                    case "!=":
                        $operator = "<>";
                        break;
                    case "<>":
                        $operator = "<>";
                        break;
                    case "LIKE":
                        $operator = "LIKE";
                        $wildcard = "%";
                        break;
                    case "<":
                        $operator = "<";
                        break;
                    case ">":
                        $operator = ">";
                        break;
                    default:
                        $operator = "";
                        break;
                }
            }
            $conditions = array("Authresource." . $field . " " . $operator => $wildcard . $value . $wildcard);
        }
        if (!$limit) {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'page' => $page);
        }
        else {
            if (!$page)
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit);
            else
                $params = array('conditions' => $conditions, 'order' => $order, 'limit' => $limit, 'page' => $page);
        }
        $this->recursive = $recursive;
        $resultsearch = $this->find('all', $params);
        return $resultsearch;
    }

}

?>